<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateBonusAPIRequest;
use App\Http\Requests\API\UpdateBonusAPIRequest;
use App\Models\Bonus;
use App\Models\BonusCategory;
use App\Repositories\BonusRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class BonusController
 * @package App\Http\Controllers\API
 */
class BonusAPIController extends ApiBaseController
{
    /** @var  BonusRepository */
    private $bonusRepository;
    protected $model_name = 'App\Models\Bonus';
    protected $model_load_array = [
    ];
    protected $search_field_array = [
        'name','bonus_category_id'
    ];

    public function __construct(BonusRepository $bonusRepo)
    {
        $this->bonusRepository = $bonusRepo;
    }

    public function getLimitBonus()
    {
        $bonus_category_id = \request('bonus_category_id');
        $bondCate = BonusCategory::where('id',$bonus_category_id)->first();
        if(!$bonus_category_id || !$bondCate){
            return responseZK(0,null,'请传正确的优惠券种类id');
        }
        $id = Auth::id();
        $ok = $this->bonusRepository->dispatchByTypeLimit($bondCate,$id);
        if($ok)
            return responseZK(1,null,'成功');
        else
            return responseZK(0,null,'超过领取数量');
    }

    public function queryLimitBonus()
    {
        $bonus_category_id = \request('bonus_category_id');
        $bond = BonusCategory::where('id',$bonus_category_id)->first();
        if(!$bonus_category_id || !$bond){
            return responseZK(0,null,'请传正确的优惠券种类id');
        }
        $id = Auth::id();
        $count = Bonus::where([
            ['bonus_category_id',$bonus_category_id],
//            ['type',2],
            ['user_id',$id],
        ])->count();
        return responseZK(1,['count'=>$count],'成功');
    }
}
